// Package handler
// Title  		validation.handler.go
// Description  验证器协调适配器
// Author  alittlexincan@163.com  2024/07/22 11:36
// Update  alittlexincan@163.com  2024/07/22 11:36
package handler

import (
	"github.com/go-kit/kit/transport/http"
	"xincan.com.cn/tarot-go/internal/app/example/validation/endpoint"
	"xincan.com.cn/tarot-go/internal/app/example/validation/service"
	"xincan.com.cn/tarot-go/internal/app/example/validation/transport"
	"xincan.com.cn/tarot-go/internal/common/response"
	"xincan.com.cn/tarot-go/internal/components/middleware"
)

var (
	// 1: 创建验证器服务
	svc = service.NewValidationService()
)

// SearchValidationObjectHandler
// Title    		SearchValidationObjectHandler
// Description   	(建议)验证器: 追加验证器链式调用(最常用), 将参数封装成结构体, 然后进行验证
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Return    		*http.Server    				查询集群节点信息
func SearchValidationObjectHandler() *http.Server {
	// 1: 创建基础endpoint
	ep := endpoint.MakeSearchValidationObjectEndpoint(svc)
	// 2: 链式调用: 验证中间件
	ep = middleware.ValidationObjectMiddleware()(ep)
	// 3: 链式调用: 日志中间件
	ep = middleware.LoggerMiddleware()(ep)
	// 4: 设置错误编码(针对业务异常)
	options := []http.ServerOption{
		http.ServerErrorEncoder(response.CustomErrorEncoder),
	}
	return http.NewServer(ep, transport.DecodeSearchValidationObjectRequest, response.EncodeResponse, options...)
}

// SearchValidationAttributeTransportHandler
// Title    		SearchValidationAttributeTransportHandler
// Description   	(不推荐)验证器: 单个值进行验证, 需要自定义验证器(系统已经内置直接使用), 两种方式, 强烈推荐使用链式调用
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Return    		*http.Server    				查询集群节点信息
func SearchValidationAttributeTransportHandler() *http.Server {
	// 1: 创建基础endpoint
	ep := endpoint.MakeSearchValidationAttributeTransportEndpoint(svc)
	// 2: 自定义解码器
	options := []http.ServerOption{
		http.ServerErrorEncoder(response.CustomErrorEncoder),
	}
	return http.NewServer(ep, transport.DecodeSearchValidationAttributeTransportRequest, response.EncodeResponse, options...)
}

// SearchValidationAttributeEndpointHandler
// Title    		SearchValidationAttributeEndpointHandler
// Description   	(不推荐)验证器: 单个值进行验证, 需要自定义验证器(系统已经内置直接使用), 两种方式, 强烈推荐使用链式调用
// Author      		alittlexincan@163.com         	2024/07/22 11:36
// Return    		*http.Server    				查询集群节点信息
func SearchValidationAttributeEndpointHandler() *http.Server {
	// 2: 创建基础endpoint
	ep := endpoint.MakeSearchValidationAttributeEndpointEndpoint(svc)

	// 此处有两种: 这两种都是要在endpoint进行验证
	// 3: 第一种: 单个属性进行验证
	//return http.NewServer(ep, transport.DecodeSearchValidationAttributeEndpointRequest, response.EncodeResponse)
	// 3: 第二种; 将单个属性组装成对象进行验证
	return http.NewServer(ep, transport.DecodeSearchValidationAttributeSetObjectEndpointRequest, response.EncodeResponse)
}
